package com.czl.module_service.viewmodel.assetUse

import androidx.databinding.ObservableField
import com.blankj.utilcode.util.GsonUtils
import com.czl.base.base.BaseBean
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.tengyun.StoreMoveBean
import com.czl.base.event.LiveBusCenter
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper
import com.czl.module_service.viewmodel.ApprovalFlowViewModel

class MyAssetStoreApplyDetailViewModel(application: MyApplication, model: DataRepository) :
    ApprovalFlowViewModel(application, model) {

    val uc = UiChangeEvent()
    var bean = ObservableField(StoreMoveBean())
    var note = ObservableField("")
    var tabIndex : Int = 1
    var moveId : Int? = null
    var taskUserId : String? = null
    class UiChangeEvent {
        val loadCompleteEvent: SingleLiveEvent<StoreMoveBean> = SingleLiveEvent()
        val backCompleteEvent: SingleLiveEvent<Any> = SingleLiveEvent()
    }

    val onBackClickCommand : BindingCommand<Any> = BindingCommand(BindingAction {
        // 撤销审批
        approveMoveApplication(moveId = moveId, status = 3)
    })

    val onAgreeClickCommand : BindingCommand<Any> = BindingCommand(BindingAction {
        // 同意
        nextTaskProcess(taskUserId = taskUserId)
    })

    val onRejectClickCommand : BindingCommand<Any> = BindingCommand(BindingAction {
        // 驳回
        hangUpTaskProcess(taskUserId = taskUserId)
    })

    fun setData(data: StoreMoveBean?) {
        data?.let {
            if(data.aduitStatus == 0) data.aduitStatusName = "待审批" else if (data.aduitStatus == 1) data.aduitStatusName = "审批通过" else if (data.aduitStatus == 2) data.aduitStatusName = "已驳回" else if (data.aduitStatus == 3) data.aduitStatusName = "已撤回"
            bean.set(data)
        }
    }

    fun getMoveInfo(
        moveId: Int? = null
    ) {
        model.mTengYunHttpDataSource.getMoveInfo(
            GsonUtils.toJson(
                mapOf(
                    "moveId" to moveId
                )
            )
        )
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<StoreMoveBean>>() {
                override fun onResult(t: BaseBean<StoreMoveBean>) {
                    if (t.state == 0) {
                        uc.loadCompleteEvent.postValue(t.data)
                    }
                    dismissLoading()
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }

            })
    }

    private fun approveMoveApplication(
        moveId: Int? = null,
        status: Int? = null
    ) {
        model.mTengYunHttpDataSource.approveMoveApplication(
            GsonUtils.toJson(
                mapOf(
                    "moveId" to moveId,
                    "userId" to model.getLocalUserInfo()?.userId,
                    "companyId" to model.getLoginCompanyId()?.toInt(),
                    "status" to status
                )
            )
        )
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .doOnSubscribe { showLoading() }
            .subscribe(object : ApiSubscriberHelper<BaseBean<Any?>>() {
                override fun onResult(t: BaseBean<Any?>) {
                    if (t.state == 0) {
                        if (status == 1) {
                            showNormalToast("审批通过")
                        }
                        if (status == 2) {
                            showNormalToast("驳回成功")
                        }
                        if (status == 3) {
                            showNormalToast("撤回成功")
                        }
                        LiveBusCenter.postAssetResultReturnEvent(1)
//                        getMoveInfo(moveId = moveId)
                        uc.backCompleteEvent.postValue(1)
                    } else {
                        showNormalToast(t.msg)
                    }
                    dismissLoading()
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }

            })
    }

    private fun nextTaskProcess(
        taskUserId: String? = null
    ) {
        model.mTengYunHttpDataSource.nextTaskProcess(
            GsonUtils.toJson(
                mapOf(
                    "taskUserId" to taskUserId,
                    "taskDesc" to note.get()
                )
            )
        )
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .doOnSubscribe { showLoading() }
            .subscribe(object : ApiSubscriberHelper<BaseBean<Any?>>() {
                override fun onResult(t: BaseBean<Any?>) {
                    if (t.state == 0) {
                        if (t.data == true) {
                            approveMoveApplication(moveId = moveId, status = 1)
                        } else {
                            showNormalToast("审批通过")
                            LiveBusCenter.postAssetResultReturnEvent(1)
                            uc.backCompleteEvent.postValue(1)
//                            getMoveInfo(moveId = moveId)
                        }
                    } else {
                        showNormalToast(t.msg)
                    }
                    dismissLoading()
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }

            })
    }
    private fun hangUpTaskProcess (
        taskUserId: String? = null
    ) {
        model.mTengYunHttpDataSource.hangUpTaskProcess(
            GsonUtils.toJson(
                mapOf(
                    "taskUserId" to taskUserId,
                    "taskDesc" to note.get()
                )
            )
        )
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .doOnSubscribe { showLoading() }
            .subscribe(object : ApiSubscriberHelper<BaseBean<Any?>>() {
                override fun onResult(t: BaseBean<Any?>) {
                    if (t.state == 0) {
                        approveMoveApplication(moveId = moveId, status = 2)
                    } else {
                        showNormalToast(t.msg)
                    }
                    dismissLoading()
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }

            })
    }

}